package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.client.Client;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oocl.dao.OrderCommentDao;
import com.oocl.dao.OrdersDao;
import com.oocl.dao.base.BaseDao;
import com.oocl.entity.OrderComment;
import com.oocl.entity.Orders;
import com.oocl.service.OrderCommentService;
import com.oocl.util.VoUtil;
import com.oocl.vo.OrderCommentVo;
import com.oocl.vo.OrderCommentVo1;

@Service(value="ordersCommentService")
public class OrdersCommentsServiceImpl implements OrderCommentService {
	
	@Resource(name="orderCommentDao")
	private OrderCommentDao ocd;
	@Resource(name="baseDao")
	private BaseDao baseDao;
	
	@Resource(name="ordersDao")
	private OrdersDao ordersDao;

	public List<OrderCommentVo1> findAllOrderComments(String icnum) {
		
		List<OrderComment> comments = ocd.getByMerchantIcnum(icnum);
		List<OrderCommentVo1> results = new ArrayList<OrderCommentVo1>();
		OrderCommentVo1 cVo1 = null;
		for(OrderComment orComment : comments){
			Orders orders = ordersDao.findOrderByOid(orComment.getOid()).get(0);
			cVo1 = new OrderCommentVo1();
			cVo1.setId(orComment.getId());
			cVo1.setClient(orders.getClient());
			cVo1.setOid(orComment.getOid());
			cVo1.setComments(orComment.getComments());
			cVo1.setOclevel(orComment.getOclevel());
			cVo1.setReply(orComment.getReply());
			cVo1.setStatus(orComment.getStatus());
			results.add(cVo1);
		}
		return results;
	}

	public List<OrderCommentVo1> findBystatuswithIcnum(String icnum, String status) {
		System.out.println(status + "----------" + icnum);
		List<OrderComment> comments = ocd.getByMerchantIcnumAndStatus(icnum, status);
		System.out.println(comments.size()+"comments size");
		List<OrderCommentVo1> results = new ArrayList<OrderCommentVo1>();
		OrderCommentVo1 cVo1 = null;
		for(OrderComment orComment : comments){
			Orders orders = ordersDao.findOrderByOid(orComment.getOid()).get(0);
			cVo1 = new OrderCommentVo1();
			cVo1.setId(orComment.getId());
			cVo1.setClient(orders.getClient());
			cVo1.setOid(orComment.getOid());
			cVo1.setComments(orComment.getComments());
			cVo1.setOclevel(orComment.getOclevel());
			cVo1.setReply(orComment.getReply());
			cVo1.setStatus(orComment.getStatus());
			results.add(cVo1);
		}
		System.out.println(results.size()+"****************************");
		return results;
	}


	@Transactional
	public OrderCommentVo replyOrderComment(String id,String reply) {
		System.out.println(id);
		System.out.println(reply);
		OrderComment oc=(OrderComment)baseDao.find(OrderComment.class, id);
		oc.setReply(reply);
		oc.setRtime(new Date());
		oc.setStatus("1");
		return VoUtil.translate(OrderCommentVo.class,(OrderComment)baseDao.update(oc));
	}

	@Override
	public OrderCommentVo1 findCommentVoByoid(String oid) {
		List<OrderCommentVo1> oList=new ArrayList<OrderCommentVo1>();
		OrderCommentVo1 ocv=new OrderCommentVo1();
		oList=ocd.findByoid(oid);
		if (oList.size()!=0) {
			ocv=oList.get(0);
		}
		return ocv;
	}

}
